from tabulate import tabulate
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from rich.style import Style
from rich import box
import random

# 生成模拟数据
data = [
    ["Python", 3.12, "动态类型", "解释型"],
    ["Java", 17, "静态类型", "编译型"],
    ["Rust", 1.0, "混合类型", "编译型"],
    ["Go", 1.21, "静态类型", "编译型"]
]

# 使用Tabulate生成基础表格字符串
tabulate_table = tabulate(
    data,
    headers=["语言", "版本", "类型系统", "执行方式"],
    tablefmt="pipe",
    floatfmt=".1f",
    numalign="right",
    stralign="left"
)

# 使用Rich进行高级渲染
console = Console()

# 创建Rich表格对象
rich_table = Table(
    title="主流编程语言对比",
    show_header=True,
    header_style="bold magenta",
    box=box.ROUNDED,
    padding=(0, 1),
    expand=True
)

# 添加带样式的列
rich_table.add_column("语言", justify="left", style="cyan", no_wrap=True)
rich_table.add_column("版本", justify="right", style="green")
rich_table.add_column("类型系统", justify="center", style="yellow")
rich_table.add_column("执行方式", justify="left", style="magenta")

# 添加数据行（保持与Tabulate相同的结构）
for row in data:
    rich_table.add_row(
        row[0],
        str(row[1]),
        row[2],
        row[3]
    )

# 添加交互式进度条
with console.status("正在分析语言特性...", spinner="dots") as status:
    # 模拟数据处理延迟
    import time
    time.sleep(2)
    
    # 添加随机评分列
    rich_table.add_column("流行指数", justify="right", style="bold blue")
    for i in range(len(data)):
        score = random.randint(70, 100)
        rich_table.add_row(
            data[i][0],
            str(data[i][1]),
            data[i][2],
            data[i][3],
            f"{score}%",
            style=Style(
                color="blue" if score > 85 else "white",
                bold=score > 85
            )
        )

# 组合输出
console.print(
    Panel.fit(
        rich_table,
        title="编程语言生态分析报告",
        border_style="bright_blue",
        padding=(1, 2)
    ),
    style="white on dark_blue"
)